<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/30
 * Time: 13:20
 */

namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function index(){
        $this->nick();
        $this->display();
    }
    //用户登录
    public function login(){

        if (IS_POST) {
            $user_nickname = $_POST['user_nickname'];//提取用户名
            $user_password = md5($_POST['user_password']); // 提取密码并进行加密
            // $user_password = $_POST['user_password']; // 提取密码并进行加密
            // 检查验证码
            $verify = I('param.verify','');
            if(!check_verify($verify)){
                $this->error("亲，验证码输错了哦！",U('login/index'),2);
            }

            //账号匹配
            if ($user = M('user')->where("user_nickname='$user_nickname' AND user_password='$user_password'")->find()) {
                $status=M('user')->where("user_nickname='$user_nickname'AND user_password='$user_password' AND user_status='正常'")->select();
                $status1=M('user')->where("user_nickname='$user_nickname'AND user_password='$user_password' AND user_status='黑名单'")->select();
                if(!empty($status)||!empty($status1)) {
                    // 写入原来登陆信息
                    /*$user_id = $user['user_id'];//提取用户id*/
                    /*  $last['login_time'] = $user['login_time']+1;//登录次数加1
                     $_SESSION['last_login_time'] = $user['last_login_time'];//提取该用户上次登录时间
                     $last['last_login_time'] = date("Y-m-d h:i:s");//记录当前登录时间
                     M('user')->where("user_id=$user_id")->save($last);//修改数据库*/
                    $user_id = $user['user_id'];//提取用户id
                    $_SESSION['user_id'] = $user_id;
                    $_SESSION['user_nickname'] = $user_nickname;
                    $this->redirect('Index/index');
                }else{
                    $this->error('账户需要激活，请登录注册邮箱进行激活！',U('login/index'),5);
                }
            }else{
                $this->error('登陆失败，用户名或密码错误！',U('login/index'),5);
            }
        }else{
            $this->error('网页出错');
        }
    }

    public function nick(){
        $user_nickname = $_SESSION['user_nickname'];
        if(isset($user_nickname)){
            $url=U('center/index');
            $url1=U('login/logout');
            $tip=$user_nickname;
            $tip1="注销";
        }
        else{
            $url=U('login/index');
            $url1="#";
            $tip="登录|注册";
            $tip1="";
        }
        $this->assign("url",$url);
        $this->assign("tip",$tip);
        $this->assign("url1",$url1);
        $this->assign("tip1",$tip1);
    }
    /**
     * 用户注册
     */
    public function register(){
        if (IS_POST) { //验证注册信息
            /*if ($this->check_user($_POST['username'] ,$_POST['password1'] , $_POST['password2'],$_POST['email'],$_POST['verify'])) {*/
            $data['user_nickname'] = $_POST['re_nickname'];
            $data['user_name'] = $_POST['re_name'];
            $data['user_IDcard'] = $_POST['re_idcard'];
            $data['user_password'] = md5($_POST['re_password']);
            $password=md5($_POST['re_password']);

            //$data['user_password'] = $_POST['re_password'];
            $data['user_email'] = $_POST['re_email'];
            $email_trim=trim($_POST['re_email']);
            $data['user_status'] = "待激活";//待激活状态
            $data['user_mobile'] = $_POST['re_mobile'];

            $regtime = date('Y-m-d H:i:s');

            $data['regtime']=$regtime;

            $id = $_POST['re_idcard'];
            $email=$_POST['re_email'];
            $nick_name=$_POST['re_nickname'];
            $record=M('user')->where("user_idcard='$id'")->count();
            $record1=M('user')->where("user_email='$email'")->count();
            $record2=M('user')->where("user_nickname='$nick_name'")->count();
            if($record==0&&$record1==0) {
                if($record2==0){
                    if ($user_id = M('user')->add($data)) {// 插入用户表
                        $stat['user_id'] = $user_id;
                        $time = date('Y-m-d H:i:s');
                        // $acc1= $row['user_account'];
                        $token =md5($time.$time);
                        $url = "http://127.0.0.1:808/hotel/index.php?m=Home&c=login&a=check_email&token=".$token;//构造URL
                        $result=sendMail1($time,$email,$url);
                        if($result==1){//邮件发送成功
                            $data['user_token']=$token;
                            $condition['user_email']=$email;
                            M('user')->where($condition)->save($data);
                            $this->success('注册成功 ,请在注册的邮件里激活登录！',U('index/index'));
                        }}
                }else {
                    $this->error('该用户名已被注册，请更换');
                }}else{
                $this->error('该身份证或者邮箱已经被注册，请重新提交注册资料');
            }}else {
            $this->error('注册失败，请检查你的注册资料！');
        }


    }

    // 退出
    public function logout(){
        session(null);
        $this->redirect('index/index');
    }
    //验证码生成
    public function verify_c(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 18;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        $Verify->codeSet = '0123456789';
        $Verify->imageW = 130;
        $Verify->imageH = 50;
        //$Verify->expire = 600;
        $Verify->entry();
    }


    public function find_pwds()
    {
        $this->nick();
//        if (IS_POST) {
        $email = $_POST['user_email'];
        $verify = I('param.verify', '');
        $repass_time=time()+60*10;//10分钟修改时间
        if (empty($email)) { // 检查是否为空
            $this->error('该邮箱不存在，请重新填写！');
        }
        if (!M('user')->where("user_email = '$email'")->find()) {
            $this->error('该邮箱不存在，请重新填写！'); // 检查账号是否存在
        } else
            if (!check_verify($verify)) {
                $this->error("亲，验证码输错了哦！", $this->site_url, 1);
            } else {// 重置密码并发送密码
                /* $result=M('user')->where("user_email='$email'")->select();
                 $row = mysql_fetch_assoc($result);*/
                // $getpasstime = time();
                // $acc=$row['user_account'];

                $time = date('Y-m-d H:i:s');
                // $acc1= $row['user_account'];
                $token =md5($time.$time);
                $url = "http://127.0.0.1:808/hotel/index.php?m=Home&c=login&a=reset&token=".$token;//构造URL
                $result=sendMail($time,$email,$url);
                if($result==1){//邮件发送成功
                    $data['repass_time']=$repass_time;
                    $data['user_token']=$token;
                    $condition['user_email']=$email;
                    M('user')->where($condition)->save($data);
                    $this->success('发送成功！请注意查收邮件！');
                }else{
                    $msg = $result;
                }
                echo $msg;
            }
    }
    public  function reset(){
        $token=$_GET['token'];
        $this->assign('token',$token);
        $res = M('user')->where("user_token='$token'")->find();
        $now=time();
        if($now<$res['repass_time']) {
            $this->display();
        }else{
            $this->error('该链接已11过期，请重新获取邮件！');
        }
    }
    public function reset_pw(){
        $token=$_POST['token'];
        $pass=md5($_POST['pass']);
        $pass1=md5($_POST['pass1']);
        if(strlen($_POST['pass'])<6||strlen($_POST['pass1'])<6){
            $this->error("密码长度不符合要求");
        }
        else if($pass!==$pass1){
            $this->error('两次密码不一致，请重新输入！');
        }else
        {
            $res = M('user')->where("user_token='$token'")->find();
            $now = time();
            if ($pass == $pass1) {
                if ($now < $res['repass_time']) {
                    $data['user_password'] = $pass1;
                    $condition['user_token'] = $token;
                    $result = M('user')->where($condition)->save($data);
                    if ($result !== false) {
                        $this->success('修改密码成功！请妥善保管你的密码', U('login/index'));
                    } else {
                        $this->error('修改密码失败！请重试！');
                    }
                } else {
                    $this->error('该链接已过期，请重新获取邮件！');
                }
            }
        }
    }
    public function check_email(){
        $token=$_GET['token'];
        $res = M('user')->where("user_token='$token'")->find();
        if(!empty($res)){
            $data['user_status'] = '正常';
            $condition['user_token'] = $token;
            $result = M('user')->where($condition)->save($data);
            if ($result !== false) {
                $this->success('激活成功！欢迎加入海悦', U('login/index'));
            } else {
                $this->error('激活失败！请重试！');
            }
        }else{
            $this->error('服务器繁忙！');
        }
    }
}